﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GmJournalApplicationEventHandler.h"
#include "IJournalCommandData.h"
#include "JournalUtils.h"
#include "GcmpApplicationInterface.h"
#include "FilePath.h"
#include "GbmpFileSystem.h"
#include "AlgorithmDebugReport.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;


CREATE_DEBUG_MODE(EnableDebugReport, L"打开DebugReport生成调试报告", gcmp::DebugModeGroup::DMGT_GEOMETRY_LIBRARY, L"gaolw", L"2023/11/07",
    L"打开DebugReport生成调试报告");

void GmJournalApplicationEventHandler::On(IApplicationEventArgs* pArgs)
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pArgs, L"pArgs为空。",L"GDMPLab",L"2024-03-30");

    ApplicationEventType uiEventType = pArgs->GetType();
    switch (uiEventType)
    {
    case ApplicationEventType::Executed:
    {
        if (DEBUG_MODE(EnableDebugReport))
        {
            static int i = 1;
            /*std::wstring fileName = L"GGP_DEBUG_ChamferBlends" + StringUtil::ToWString(i++);*/
            const LogManager* pLogManager = LogManager::Get();
            DBG_WARN_AND_RETURN_VOID_UNLESS(pLogManager, L"pLogManager为空指针",L"GDMPLab",L"2024-03-30");
            std::wstring logFolderPath = pLogManager->GetLogsFolder();
            // 这里取Log本身folder的名字作为调试报告的名字
            size_t pos = logFolderPath.find_last_of(FileSystem::Separator());
            std::wstring logFolderName = logFolderPath.substr(pos + 1);
            std::wstring filePath = FilePath::Combine(pLogManager->GetLogsFolder(), logFolderName);
            bool start = AlgorithmDebugReport::StartReport(filePath, StartDebugReportOptions());
            DBG_WARN_AND_RETURN_VOID_UNLESS(start, L"未能开启调试报告",L"GDMPLab",L"2024-03-30");
        }
        break;
    }
    case ApplicationEventType::PreClose:
    {
        if (pArgs->IsApplicationClosedByUser())
        {
            OwnerPtr<gcmp::IJournalCommandData> opJournalCmdData = IJournalCommandData::Create();
            DBG_WARN_AND_RETURN_VOID_UNLESS(opJournalCmdData, L"IJournalCommandData::Create()返回空指针",L"GDMPLab",L"2024-03-30");
            opJournalCmdData->SetCommandName(ID_CMD_APPLICATION_CLOSE);
            JournalUtils::RecordCommand(TransferOwnership(opJournalCmdData));
        }

        if (DEBUG_MODE(EnableDebugReport))
        {
            bool stop = AlgorithmDebugReport::StopReport();
            DBG_WARN_AND_RETURN_VOID_UNLESS(stop, L"未能关闭调试报告",L"GDMPLab",L"2024-03-30");
        }

        break;
    }

    }
    return;
}

